import 'dart:collection';

import 'package:flutter/cupertino.dart';
import 'package:get/get.dart';
import 'package:xiaomishop/app/data/good_detail_model.dart';
import 'package:xiaomishop/app/net/HttManager.dart';

import '../../../net/Url.dart';

class GooddetailController extends GetxController {
  //TODO: Implement GooddetailController
  ScrollController scrollController = ScrollController();

  RxDouble withOpacity = 0.0.obs;
  GlobalKey key1 = GlobalKey();
  GlobalKey key2 = GlobalKey();
  GlobalKey key3 = GlobalKey();
  GlobalKey appBarKey = GlobalKey();

  GlobalKey containerKey = GlobalKey();

  Rx<Detail> detail = Detail().obs;

  // RxList<Map> pAttr = [{}].obs;

  final titleMap = [
    {"id": "1", "title": "商品"},
    {"id": "2", "title": "详情"},
    {"id": "3", "title": "评价"}
  ];
  Rx<String> titleId = "1".obs;

  RxBool showTab = false.obs;

  void setCurrentTitle(String id) {
    titleId.value = id;
    update();
  }

  final String name = "11";

  void setName() {
    print('hello world name');
    Map<String, GoodDetailModel> map = HashMap<String, GoodDetailModel>();
  }

  @override
  void onInit() {
    super.onInit();

    print('GooddetailController onInit');
    WidgetsBinding.instance!.addPostFrameCallback(_afterLayout);
    initListener();
    print("id = ${Get.arguments["id"]}");
    initGoodDetail();
  }

  double h1 = 0;
  double h2 = 0;
  double h3 = 0;
  double appBarH = 0;

  /**
   * 获取SingleChildScrollView 各个元素的高度
   */
  void _afterLayout(_) {
    final RenderBox? containerRenderBox1 =
        key1.currentContext?.findRenderObject() as RenderBox?;
    if (containerRenderBox1 != null) {
      h1 = containerRenderBox1.size.height;
    }
    final RenderBox? containerRenderBox2 =
        key2.currentContext?.findRenderObject() as RenderBox?;
    if (containerRenderBox2 != null) {
      h2 = containerRenderBox2.size.height;
    }
    final RenderBox? containerRenderBox3 =
        key3.currentContext?.findRenderObject() as RenderBox?;
    if (containerRenderBox3 != null) {
      h3 = containerRenderBox3.size.height;
    }

    final RenderBox? containerRenderBox4 =
        appBarKey.currentContext?.findRenderObject() as RenderBox?;
    if (containerRenderBox4 != null) {
      appBarH = containerRenderBox4.size.height;
    }

    print('_afterLayout ${h1} ${h2} ${h3} ${appBarH}');
  }

  @override
  void onReady() {
    super.onReady();
    print('GooddetailController onReady');
  }

  @override
  void onClose() {
    super.onClose();
    print('GooddetailController onClose');
  }

  void initListener() {
    scrollController.addListener(() {
      var pixels = scrollController.position.pixels;
      print('${pixels}');
      withOpacity.value = pixels / appBarH;
      if (withOpacity.value > 1) {
        withOpacity.value = 1;
      }
      if (withOpacity.value >= 0.1) {
        if (showTab.value == false) {
          showTab.value = true;
        }
      } else {
        if (showTab.value == true) {
          showTab.value = false;
        }
      }
      if (showTab.value) {
        if (pixels < h1 - appBarH) {
          setCurrentTitle("1");
        } else if (pixels > h1 - appBarH && pixels < h2 + h1 - appBarH) {
          setCurrentTitle("2");
        }
        if (pixels > h2 + h1 - appBarH) {
          setCurrentTitle("3");
        }
      }

      update();
    });
  }

  void initGoodDetail() async {
    var rsp = await HttManager.instance
        .get(Url.API_PCONTENT, map: {"id": "${Get.arguments["id"]}"});
    if (rsp != null && rsp.data != null) {
      detail.value = GoodDetailModel.fromJson(rsp.data).result;
      initPAttr();
    }
  }

  void initPAttr() {
    detail.value.pAttr!.clear();
    for (int i = 0; i < detail.value.attr!.length; i++) {
      var element1 = detail.value.attr![i];
      List<Map> mapList = [];
      for (int j = 0; j < element1.list.length; j++) {
        var element2 = element1.list[j];
        if (j == 0) {
          var map2 = {"title": element2, "checked": true};
          mapList.add(map2);
        } else {
          var map2 = {"title": element2, "checked": false};
          mapList.add(map2);
        }
      }
      var attrMap = {"cate": element1.cate, "list": mapList};
      detail.value.pAttr!.add(attrMap);
    }
    print("adsdasdasd ${detail.value.pAttr!.length}");
    update();
  }

  /**
   * 切换选中状态
   */
  void changeChecked(cate, title) {
    print('${cate} and ${title}');
    for (int i = 0; i < detail.value.pAttr!.length; i++) {
      var map1 = detail.value.pAttr![i];
      String mCate = map1["cate"];
      List<Map> mList = map1["list"];
      for (int j = 0; j < mList.length; j++) {
        var map2 = mList[j];
        String mTitle = map2["title"];
        if (cate == mCate) {
          if (title == mTitle) {
            map2["checked"] = true;
          } else {
            map2["checked"] = false;
          }
        }
      }
    }
    detail.refresh();
    update();
  }
}
